if(condition)Here, condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not.
{
// Statements to execute if
// condition is true
}
if(condition)Flowchart:
statement1;
statement2;
// Here if the condition is true, if block
// will consider only statement1 to be inside
// its block.

I am Not in ifAs the condition present in the if statement is false. So, the block below the if statement is not executed.
if (condition)Flowchart:
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

i is greater than 15The block of code following the else statement is executed as the condition present in the if statement is false.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}

i is smaller than 15Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
i is smaller than 12 too
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;

i is 20Switch case statements are a substitute for long if statements that compare a variable to several integral values.
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}

Choice is 2
Looping Statements
Iterative Method
An iterative method to do this is to write the cout statement 10 times.Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Using Loops
In Loop, the statement needs to be written only once and the loop will be executed 10 times as shown below.for Loop
A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line.for (initialization expr; test expr; update expr)In for loop, a loop variable is used to control the loop. First, initialize this loop variable to some value, then check whether this variable is less than or greater than counter value. If statement is true, then loop body is executed and loop variable gets updated. Steps are repeated till exit condition comes.
{
// body of the loop
// statements we want to execute
}

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
While Loop
While studying for loop we have seen that the number of iterations is known beforehand, i.e. the number of times the loop body is needed to be executed is known to us. while loops are used in situations where we do not know the exact number of iterations of loop beforehand. The loop execution is terminated on the basis of the test condition.initialization expression; while (test_expression)
{
// statements
update_expression; }

Hello World
Hello World
Hello World
Hello World
Hello World
do while loop
In do while loops also the loop execution is terminated on the basis of test condition. The main difference between do while loop and while loop is in do while loop the condition is tested at the end of loop body, i.e do while loop is exit controlled whereas the other two loops are entry controlled loops.initialization expression; doNote: Notice the semi - colon(";") in the end of loop.
{
// statements
update_expression; } while (test_expression);

Hello World
What about an Infinite Loop?
An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a functional exit so that it repeats indefinitely. An infinite loop occurs when a condition always evaluates to true. Usually, this is an error.This loop will run forever.
This loop will run forever.
...................
Jump Statements
Jump statements help programmers to jump directly to a point in program breaking the normal flow of execution. They provide change in program execution flow. There are 3 jump constructs in C/C++:
5 found in array

1 2 3 4 5 7 8 9 10
Syntax1 | Syntax2As can be seen from the above syntax, the label can exist anywhere in the program (prior or after). Upon encountering the goto label; statement, flow jumps to the label unconditionally.
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;

1 2 3 4 5
< return-type > < function-name > (< set-of-arguments >) {
//block of statements
}
As an example:Maximum of a & b is: 20
Prototype Declaration & Definition
Sometimes, we just want to declare the existence of a function and provide the implementation afterwards. We can do so by using the prototype-declaration scheme, the syntax of which is given below:< return-type > < function-name > (< set-of-arguments >);As an example:
Formal & Actual Parameters
The parameters passed to function are called actual parameters. For example, in the first program 10 and 20 are actual parameters. The parameters received by function are called formal parameters. For example, in the above program x and y are formal parameters.Pass by value & Pass by Reference
There are two types of passing arguments to functions. The arguments declared within the function (formal parameter variables) have function block scope only. Hence, any manipulation done on them has no effect on the actual variables with which the function was called. This will be clear with the following Pass by value example:a: 5, b: 6As we can see, the values inside of main don't get swapped. This is because they were passed by value. Hence, any change done inside the function isn't reflected in the main() part. This is because in pass-by-value, the value and not the actual memory location gets copied to the formal parameters in the function.
int swap(int &a, int &b) { ... }
//add & before the parameters (call-by-reference)
...
int a = 5, b = 6;
swap(a, b); //calling the function remains the same
a: 6, b: 5So, the elements get swapped in real.
Inline Functions
Inline functions are an improvement provided by C++ to reduce the overhead caused in executing a function call. A function call statement is a jump statement that instructs the program counter to switch to a different address for execution. In a function call, a lot of work that the OS needs to be done such as storing the current address of execution (to return back to), along with other registers. Also, allocation and de-allocation (upon exit) of variables local to the functions are done. This can greatly hamper performance if a function is called repeatedly in a program. e.g. frequently called utility functions such as min(), max(), or in case of recursive functions such as factorial().6
8
cout << (5 > 6) ? 5 : 6 << endl;If there are multiple calls to max_int function, usage of inline can greatly improve performance. However, there is an added downside of increased compilation code size, because of inline-expansion. Thus, it is advised to make only those functions inline which are short & frequently used.
cout << (8 > 7) ? 8 : 7 << endl;
Default Arguments
A Default Argument is a default value for a function argument. In case the user forgets to provide the parameter, the default value for that variable would be used. As an example:11
5
int add(int y=10, int x); //not allowedas a call to a function like add( , 3) is not allowed in C++. Rather add(3) is valid. So, all default params should follow the non-default ones.
int add(int x, int y=10); //allowed
Function Overloading
Function overloading allows us to create functions with different names, as long as they have different parameters. As an example:11
Hello World
Function Pointers
Functions are an executable block of code which is placed at a certain address when a program is loaded into memory. e.g. If we execute the code below:0x002717f0
< return-type > (* < function-name >)(< argument-list >);Example:
11
Hello World
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
Variable Arguments
One might have seen functions such as printf, scanf which are capable of printing/scanning any number of input arguments provided. This is achieved by using variable arguments. The advantage of having a variable argument feature in C++ is to have functions which can operate on any number of arguments and produce the result. e.g. Suppose we want a function that can take the average of any number of integers passed:6.95312e-310
4.16905e-310
Asked In: FactSet MAQ Software Morgan Stanley Samsung Wipro